﻿Imports DevComponents.DotNetBar

Public Class ucCapnhatnhasx

    Event Closed(sender As Object)

    Private eActive As mdQuanLyBanHang.ActionType
    Private dtrow As DataGridViewRow
    Private manufacBUS As New ManufactureBUS()

    Public _isCancelled As Boolean = True
    Public Property Cancelled() As Boolean
        Get
            Return _isCancelled
        End Get
        Private Set(ByVal value As Boolean)
            If value <> _isCancelled Then
                _isCancelled = value
            End If
        End Set
    End Property

    Private Sub ButtonItemThoat_Click(sender As Object, e As EventArgs) Handles ButtonItemThoat.Click
        SuperValidator1.Enabled = False
        Dim uc As New ucCapnhatnhasx(2, dtrow)
        Me.Controls.Add(uc)
        Dim parent = DirectCast(Application.OpenForms.Item(0), DevComponents.DotNetBar.Metro.MetroAppForm)
        parent.CloseModalPanel(uc, DevComponents.DotNetBar.Controls.eSlideSide.Right)

        'uc.Dispose()

        Cancelled = True
        RaiseEvent Closed(Me)

    End Sub

    Private Sub ButtonItemSave_Click(sender As Object, e As EventArgs) Handles ButtonItemSave.Click
        Dim flatReturn As Boolean = False
        If ValidateData() Then
            Try
                BalloonTip1.SetBalloonCaption(ItemPanel1, "Thông báo")
                If eActive = ActionType.AddRecord Then
                    manufacBUS.Manufacturers_Insert(txtTennhasx.Text, txtMota.Text, Now, "Current user", Now, "Name's Loging User")
                    BalloonTip1.SetBalloonText(ItemPanel1, "Thêm nhà sản xuất thành công")
                    'MessageBoxEx.Show("true", "test", MessageBoxButtons.OK)
                ElseIf eActive = ActionType.EditRecord Then
                    flatReturn = manufacBUS.Manufacturers_Update(Integer.Parse(dtrow.Cells(0).Value), _
                                                    txtTennhasx.Text, _
                                                    txtMota.Text, _
                                                    Date.Parse(dtrow.Cells(3).Value), _
                                                    dtrow.Cells(4).Value.ToString(), _
                                                    Now, _
                                                    "Name's User")
                    If flatReturn = True Then
                        BalloonTip1.SetBalloonText(ItemPanel1, "Cập nhật nhà sản xuất thành công")
                        BalloonTip1.ShowBalloon(ItemPanel1)
                    End If

                    'MessageBoxEx.Show("false", "test", MessageBoxButtons.OK)
                End If
            Catch ex As Exception

            End Try

            'MessageBoxEx.Show("true", "test", MessageBoxButtons.OK)
            Cancelled = False
            RaiseEvent Closed(Me)
        Else
            'MessageBoxEx.Show("false", "test", MessageBoxButtons.OK)
        End If
    End Sub

    Private Function ValidateData() As Boolean
        Dim validate As Boolean = True
        If txtTennhasx.Text = "" Then
            validate = False
            ErrorProvider1.SetError(txtTennhasx, "Vui lòng nhập tên nhà sản xuất")
        Else
            ErrorProvider1.SetError(txtTennhasx, "")
        End If
        Return validate
    End Function

    Public Sub New(ByVal eAct As Integer, ByVal _dtrow As DataGridViewRow)
        MyBase.New()
        InitializeComponent()
        eActive = eAct
        dtrow = _dtrow

        If eActive = 0 Then
            lbTieude.Text = "Thêm nhà sản xuất mới"

        ElseIf eActive = 1 Then
            lbTieude.Text = "Cập nhật nhà sản xuất"
            txtTennhasx.Text = dtrow.Cells(1).Value.ToString()
            txtMota.Text = dtrow.Cells(2).Value.ToString()
        End If
    End Sub

End Class
